package com.yang.ddd.interfaces.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.ddd.application.service.SpaceApplicationService;
import com.yang.ddd.application.service.UserApplicationService;
import com.yang.ddd.domain.space.entity.Space;
import com.yang.ddd.domain.space.entity.SpaceLevel;
import com.yang.ddd.domain.user.constant.UserConstant;
import com.yang.ddd.domain.user.entity.User;
import com.yang.ddd.infrastructure.annotation.AuthCheck;
import com.yang.ddd.infrastructure.common.BaseResponse;
import com.yang.ddd.infrastructure.common.DeleteRequest;
import com.yang.ddd.infrastructure.common.ErrorCode;
import com.yang.ddd.infrastructure.common.ResultUtils;
import com.yang.ddd.infrastructure.exception.BusinessException;
import com.yang.ddd.interfaces.dto.space.SpaceAddRequest;
import com.yang.ddd.interfaces.dto.space.SpaceEditRequest;
import com.yang.ddd.interfaces.dto.space.SpaceQueryRequest;
import com.yang.ddd.interfaces.dto.space.SpaceUpdateRequest;
import com.yang.ddd.interfaces.vo.space.SpaceVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 空间接口
 */
@RestController()
@RequestMapping("/space")
@Slf4j
public class SpaceController {

    @Resource
    private UserApplicationService userApplicationService;

    @Resource
    private SpaceApplicationService spaceApplicationService;


    /**
     * 分页获取空间列表（仅管理员可用）
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation("分页获取空间列表（仅管理员可用）")
    public BaseResponse<Page<Space>> listSpaceByPage(@RequestBody SpaceQueryRequest spaceQueryRequest) {
        return ResultUtils.success(spaceApplicationService.listSpaceByPage(spaceQueryRequest));
    }


    /**
     * 分页获取空间列表-封装类
     */
    @PostMapping("/list/page/vo")
    @ApiOperation("分页获取空间列表-封装类")
    public BaseResponse<Page<SpaceVO>> listSpaceVOByPage(@RequestBody SpaceQueryRequest spaceQueryRequest) {
        return ResultUtils.success(spaceApplicationService.listSpaceVOByPage(spaceQueryRequest));
    }

    /**
     * 获取所有空间级别
     * @return
     */
    @GetMapping("/list/level")
    @ApiOperation("空间级别列表")
    public BaseResponse<List<SpaceLevel>> listSpaceLevel() {
        return ResultUtils.success(spaceApplicationService.listSpaceLevel());
    }

    /**
     * 创建空间
     *
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("创建空间")
    public BaseResponse<Long> addSpace(@RequestBody SpaceAddRequest spaceAddRequest, HttpServletRequest request) {
        if (spaceAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userApplicationService.getLoginUser(request);
        long spaceId = spaceApplicationService.addSpace(spaceAddRequest, loginUser);
        return ResultUtils.success(spaceId);
    }


    /**
     * 根据 id 获取空间信息
     */
    @GetMapping("/get/vo")
    @ApiOperation("根据 id 获取空间信息")
    public BaseResponse<SpaceVO> getSpaceVOById(long id, HttpServletRequest request) {
        return ResultUtils.success(spaceApplicationService.getSpaceVOById(id, request));
    }


    /**
     * 删除空间
     */
    @PostMapping("/delete")
    @ApiOperation("删除空间")
    public BaseResponse<Boolean> deleteSpace(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        return ResultUtils.success(spaceApplicationService.deleteSpace(deleteRequest, request));
    }

    /**
     * 编辑空间（给用户使用）
     */
    @PostMapping("/edit")
    @ApiOperation("编辑空间（给用户使用）")
    public BaseResponse<Boolean> editSpace(@RequestBody SpaceEditRequest spaceEditRequest, HttpServletRequest request) {
        spaceApplicationService.editSpace(spaceEditRequest, request);
        return ResultUtils.success(true);
    }


    /**
     * 更新空间（给管理员使用）
     * @param spaceUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新空间（给管理员使用）")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateSpace(@RequestBody SpaceUpdateRequest spaceUpdateRequest) {
        spaceApplicationService.updateSpace(spaceUpdateRequest);
        return ResultUtils.success(true);
    }

}